package com.my.study.controller;

import com.alibaba.fastjson.JSONObject;
import com.my.study.aop.anntation.FileTypeCheck;
import com.my.study.aop.anntation.OperationLog;
import com.my.study.aop.anntation.Permission;
import com.my.study.entity.dto.RequestDTO;
import com.my.study.entity.vo.ResponseVO;
import com.my.study.entity.vo.UserInfoVO;
import com.my.study.enums.BusinessExceptionEnum;
import com.my.study.enums.CheckTypeEnum;
import com.my.study.enums.RoleTypeEnum;
import com.my.study.excel.demo.TestExcelExportDemo;
import com.my.study.exception.BusinessException;
import com.my.study.utils.ExcelReadUtil;
import com.my.study.utils.RedisUtil;
import com.my.study.utils.RedissonLockUtil;
import com.my.study.utils.ThreadExecutorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/check")
@Slf4j
public class CheckHealthController {

    @Autowired
    private TestExcelExportDemo testExcelExportDemo;

    @GetMapping("/test")
    @OperationLog(operationContent = "test")
    @Permission(roles = {RoleTypeEnum.SECURITY_ADMIN})
    public ResponseVO test(@RequestBody RequestDTO requestDTO) {
        return ResponseVO.success(requestDTO);
    }

    @GetMapping("/test1")
    public ResponseVO test1() {
        List<String> tempList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            tempList.add("test" + i);
        }
        List result = ThreadExecutorUtil.batchExecutor(tempList, list -> {
            List<JSONObject> objects = new ArrayList<>();
            list.forEach(s -> {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("test", "test");
                objects.add(jsonObject);
            });
            return objects;
        });
        return ResponseVO.success(result);
    }

    @GetMapping("/test2")
    public ResponseVO test2() {
        List<Map<String, String>> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("key" + i, "val" + i);
            list.add(map);
        }
        ThreadExecutorUtil.batchSendEmail(list, () -> {
            list.forEach(stringStringMap -> log.info(stringStringMap.toString()));
        });
        return ResponseVO.success();
    }

    @GetMapping("/test3")
    public ResponseVO test3() {
        ThreadExecutorUtil.Test((code, msg) -> {
            log.info("1111111111");
        });
        return ResponseVO.success(RedisUtil.getValue("test", UserInfoVO.class));
    }

    @GetMapping("/test4")
    public ResponseVO test4() {
        String result = RedissonLockUtil.deduceStock("test",20, () -> {
            log.info("------ 执行业务自定义逻辑逻辑 ----end------");
            return "success";
        });
        return ResponseVO.success(result);
    }

    @GetMapping("/test5")
    @OperationLog(operationContent = "test")
    @Permission(roles = {RoleTypeEnum.SECURITY_ADMIN})
    @FileTypeCheck(suffix = {".xlsx", ".xls"}, checkTypeEnum =
            {CheckTypeEnum.FILE_SUFFIX, CheckTypeEnum.FILE_SIZE}, size = 50000)
    public ResponseVO test5(MultipartFile file) {
        try {
            return ResponseVO.success(ExcelReadUtil.readExcelData(file.getInputStream(), UserInfoVO.class));
        } catch (IOException exception) {
            log.error("Excel file read failed." + exception);
            throw BusinessException.fail(BusinessExceptionEnum.FAIL.code, "Excel文件读取失败！");
        }
    }

    @GetMapping("/test6")
    public ResponseVO test6(@RequestBody RequestDTO requestDTO) {
        return ResponseVO.success(requestDTO);
    }

    @GetMapping("/check-health")
    public ResponseVO checkHealth() {
        return ResponseVO.success();
    }

    @GetMapping("/export")
    public ResponseVO export(HttpServletResponse response) {
        testExcelExportDemo.export(response,new HashMap<>(),"test");
        return ResponseVO.success();
    }
}
